Parameter-sensitive plans

ABSTRACT

A query configured according to a query template is received. The query includes values for a plurality of parameters. A key is generated for the query that indicates a selectivity region in a selectivity space determined for the included parameter values. Whether the generated key matches any key of a plurality of keys is determined. Each key of the plurality of keys is associated with a corresponding query plan. If the generated key is determined to not match any of the keys, the query is compiled to generate a new query plan, and the generated query plan is executed with respect to the query. If the key is determined to match any key of the plurality of keys, a query plan associated with the matched key is selected, and the query plan associated with the matched key is executed with respect to the query.

BACKGROUND

A database functions as an organized collection of information.Electronic data may be stored in a database to facilitate rapid dataretrieval. Data is typically organized within a database in one or moretables, with the tables being arranged as an array of rows and columns.A table can include a set of records, with each record including a setof fields. Records are typically indexed as rows within a table, and thefields are typically indexed as columns, such that a row/column pair ofindices can reference particular datum within a table. For example, arow can store a complete data record relating to a sales transaction, aperson, a project, etc. Likewise, columns of the table can definediscrete portions of the rows that have the same general data formatsuch that the columns define fields of the records. Databaseapplications enable users to compare, sort, order, merge, separate, andinterconnect data stored in databases.

A query may be issued to a database to access data stored therein.Queries in database systems are typically posed in high level,declarative (non-procedural) languages that are translated into aprocedural execution plan. Numerous query languages exist, including SQL(Structured Query Language) and others. Many relational databasemanagement systems (RDBMSs) use query optimizers to select a query planfor a received query. The purpose of query optimization is to determineone or more ways in which the query can be translated into proceduralplans, and to select the most efficient plan among the determined plans.The selected query execution plan typically includes a series ofprimitive database operators, and is typically selected according to aleast estimated execution cost.

In RDBMSs, users issue queries against data that is stored in softwareconstructs. When a query is received from a user, the RDBMS compiles thequery by generating a query plan that can be executed against thedatabase. As described above, for any such query, there are a variety ofdifferent query plans that can be appropriately used. For parameterizedqueries, different query plans may be more efficient depending on theparticular values used for the parameters of the parameterized query.Some query plans will execute the query with a low cost, while otherquery plans will execute the query with a high cost. In order to avoidsubsequent compilations of the same query, a query plan may be stored inmemory so that the query plan can be quickly brought back for execution.

A particular query plan may be generated based on factors other thanjust the query text, such as available memory. Although each query planmay be valid across all values of those factors, plan reuse does notguarantee that an executed plan is currently optimal. Parameter valuesmay impact the choice of access paths and the size of intermediateresults, leading to different optimal query plans. One query plangeneration strategy is to compile a query plan assuming parameter valuessubmitted at the first execution. In this manner, an optimal query planis used at the first execution. The compiled query plan may not beoptimal for subsequent queries, however. Unfortunately, the use ofsuboptimal query plans may be prohibitive when queries processdramatically different numbers of rows due to different parametervalues.

SUMMARY

This Summary is provided to introduce a selection of concepts in asimplified form that are further described below in the DetailedDescription. This Summary is not intended to identify key features oressential features of the claimed subject matter, nor is it intended tobe used to limit the scope of the claimed subject matter.

Methods, systems, and computer program products are described herein forprocessing parameterized queries. A received query is compared withstored query plans to determine whether any of the stored query plansare suitable for being executed on the received query. The comparison ismade based on selectivities of relational subexpressions of the receivedquery involving the parameter values included in the received query. Aselectivity space is partitioned in a grid fashion, and each query planis associated with one or more selectivity regions/cells of the grid. Ifthe selectivities of the subexpressions evaluated for the receivedparameter values fall in a particular selectivity region having anassociated query plan, the query plan associated with that selectivityregion is used to execute the received query. If the selectivities fallin a selectivity region that does not have an associated query plan, anew query plan is generated based on the received parameter values. Thenew query plan is associated with the selectivity region of the receivedquery, and the new query plan is used to execute the received query.

For instance, in one implementation, a method for processing queries isprovided. A first query is received that is configured according to aquery template. The first query includes values for a plurality ofparameters. A dispatcher is generated for the query template if adispatcher is determined to not be present. A selectivity space isdefined as an array of selectivity regions arranged according to alogarithmic-scale grid that has a number of dimensions equal to a numberof subexpressions of the first query that involve parameters (e.g., alsoreferred to as “efficiency expressions,” “predicates,” or“parameter-sensitive predicates”). A first selectivity region in theselectivity space is determined for the first query using thedispatcher. A first key is generated for the first query that includesan indication of the first selectivity region. The first key isassociated with the first selectivity region. The first query iscompiled to generate a first query plan. The first query plan is storedin association with the first key. The first query plan is executed withrespect to the first query.

A subsequent query may be received that is configured according to thequery template. The subsequent query may be the next query receivedafter the first query, or any other later received query. The subsequentquery includes values for the plurality of parameters. Based on thesupplied parameter values, an appropriate selectivity region in theselectivity space is determined for the subsequent query. A key isgenerated for the subsequent query that includes the determinedselectivity region. Whether the generated key matches any stored key,including the first key, is determined A stored query plan is executedwith respect to the subsequent query if the generated key is determinedto match the key of the stored query plan. If the generated key isdetermined to not match any stored key, the subsequent query is compiledto generate a corresponding query plan, and the generated query plan isexecuted with respect to the subsequent query.

Furthermore, if the subsequent key was determined to not match anystored key, the generated query plan may be stored in association withthe subsequent key. In one implementation, the dispatcher may store amapping between keys and selectivity regions, so that a key does notnecessarily include an indication of a single selectivity region but maybe associated with one or more selectivity regions. The generated queryplan may be stored in association with a new key if the query plan isdetermined to not match any stored query plan. Also, the mapping may beupdated to associate the selectivity region with the key for the queryplan. The mapping may be updated to associate the selectivity regionwith a stored key if the obtained query plan is determined to match therespective stored query plan.

In another implementation, a query processing system is provided. Thequery processing system includes a query interface, a dispatchergenerator, and a cache. The query interface is configured to receivequeries that include values for a plurality of parameters configuredaccording to a query template. The dispatcher generator is configured togenerate a dispatcher if a dispatcher is determined to not be presentfor the query template. The dispatcher includes a query compiler caller,a key generator, and a selectivity region mapper. The cache stores thedispatcher. The selectivity region mapper is configured to map the firstquery to a first selectivity region in a selectivity space. Theselectivity space has a number of dimensions equal to a number ofefficiency expressions of the first query, and includes an array ofselectivity regions arranged according to a regular grid. The keygenerator is configured to generate a first key for the first query thatincludes an indication of the first selectivity region. The querycompiler caller is configured to request the first query to be compiledto generate a first query plan. The first query plan is stored in thecache in association with the first key. The first query plan isexecuted with respect to the first query.

Furthermore, the dispatcher may include a query plan selector. The queryplan selector is configured to lookup query plans stored in the cachefor received queries. The query interface receives a second queryconfigured according to the query template. The selectivity regionmapper is configured to map the second query to a second selectivityregion in the selectivity space. The key generator is configured togenerate a second key for the second query that includes an indicationof the second selectivity region. The query plan selector is configuredto determine whether the second key matches any key stored in the cache.If the second key is determined to match a key stored in the cache, thequery plan selector selects the query plan corresponding to a matchedkey for execution with respect to the second query.

If the query plan selector determines that the second key does not matchany key stored in the cache, the query compiler caller is configured torequest the second query to be compiled to generate a second query plan,and the second query plan is executed with respect to the second query.The second query plan may be stored in the cache in association with thesecond key.

Computer program products are also described herein that enable theprocessing of received queries, that enable the generating and managingof selectivity regions and query plans, and that enable furtherembodiments as described herein.

Further features and advantages of the invention, as well as thestructure and operation of various embodiments of the invention, aredescribed in detail below with reference to the accompanying drawings.It is noted that the invention is not limited to the specificembodiments described herein. Such embodiments are presented herein forillustrative purposes only. Additional embodiments will be apparent topersons skilled in the relevant art(s) based on the teachings containedherein.

BRIEF DESCRIPTION OF THE DRAWINGS/FIGURES

The accompanying drawings, which are incorporated herein and form a partof the specification, illustrate the present invention and, togetherwith the description, further serve to explain the principles of theinvention and to enable a person skilled in the pertinent art to makeand use the invention.

FIG. 1 shows a block diagram of a data communication system, accordingto an example embodiment.

FIG. 2 shows a block diagram of a query processing system, according toan example embodiment.

FIG. 3 shows a flowchart for processing queries, according to an exampleembodiment.

FIG. 4 shows the query processing system of FIG. 2 processing an examplereceived query, according to an embodiment.

FIG. 5 shows a block diagram of a query template, according to anexample embodiment.

FIG. 6 shows a block diagram of a dispatcher, according to an exampleembodiment.

FIG. 7 shows a graphical representation of a selectivity space,according to an example embodiment.

FIG. 8 shows a block diagram of a key, according to an exampleembodiment.

FIG. 9 shows a flowchart for processing subsequently received queries,according to an example embodiment.

FIG. 10 shows a block diagram of a cache memory, according to an exampleembodiment.

FIG. 11 shows a process for executing a previously existing query planfor a received query, according to an example embodiment.

FIG. 12 shows a flowchart for generating and executing a query plan fora received query, according to an example embodiment.

FIG. 13 shows a flowchart for managing selectivity regions, according toan example embodiment.

FIG. 14 shows a block diagram of an example computer that may be used toimplement embodiments of the present invention.

The features and advantages of the present invention will become moreapparent from the detailed description set forth below when taken inconjunction with the drawings, in which like reference charactersidentify corresponding elements throughout. In the drawings, likereference numbers generally indicate identical, functionally similar,and/or structurally similar elements. The drawing in which an elementfirst appears is indicated by the leftmost digit(s) in the correspondingreference number.

DETAILED DESCRIPTION

I. Introduction

The present specification discloses one or more embodiments thatincorporate the features of the invention. The disclosed embodiment(s)merely exemplify the invention. The scope of the invention is notlimited to the disclosed embodiment(s). The invention is defined by theclaims appended hereto.

References in the specification to “one embodiment,” “an embodiment,”“an example embodiment,” etc., indicate that the embodiment describedmay include a particular feature, structure, or characteristic, butevery embodiment may not necessarily include the particular feature,structure, or characteristic. Moreover, such phrases are not necessarilyreferring to the same embodiment. Further, when a particular feature,structure, or characteristic is described in connection with anembodiment, it is submitted that it is within the knowledge of oneskilled in the art to effect such feature, structure, or characteristicin connection with other embodiments whether or not explicitlydescribed.

Furthermore, it should be understood that spatial descriptions (e.g.,“above,” “below,” “up,” “left,” “right,” “down,” “top,” “bottom,”“vertical,” “horizontal,” etc.) used herein are for purposes ofillustration only, and that practical implementations of the structuresdescribed herein can be spatially arranged in any orientation or manner.

II. Example Embodiments

Embodiments of the present invention relate to the processing of queriesissued to databases to obtain data. For example, FIG. 1 shows a blockdiagram of a data communication system 100, according to an exampleembodiment. As shown in FIG. 1, system 100 includes a computer system102, a server 104, a network 116, and a database 114. Database 114 iscoupled to server 104. Computer system 102 and server 104 arecommunicatively coupled by network 116.

Computer system 102 be any type of computing device, including a desktopcomputer (e.g., a personal computer), a server, a mobile computer orcomputing device (e.g., a Palm® device, a RIM Blackberry® device, apersonal digital assistant (PDA), a laptop computer, a notebookcomputer, etc.), or other type of computer system. Server 104 may be anytype of computing device described herein or otherwise known that isconfigured to process database queries.

As shown in FIG. 1, database 114 may include storage 118 configured tostore data. For example, data of database 114 may be stored in storage118 in the form of one or more tables, with the tables being arranged asan array of rows and columns. For instance, a table can include a set ofrecords, with each record including a set of fields. The records may beindexed as rows within a table, and the fields may be indexed ascolumns, such that a row/column pair of indices can reference particulardatum within a table. Data of database 114 may alternatively be storedin storage 118 in other ways, in embodiments. Storage 118 may includeone or more of any type of storage mechanism to store content (e.g.,objects), including a hard disk drive, an optical disc drive, a memorydevice such as a RAM device, a ROM device, etc., and/or any othersuitable type of storage medium. Storage 118 may include long termstorage (e.g., a hard disk drive) or temporary storage (e.g., a RAMdevice).

Network 116 may include one or more communication links and/orcommunication networks, such as a PAN (personal area network), a LAN(local area network), a WAN (wide area network), or a combination ofnetworks, such as the Internet. First and second communication links 122and 124, which respectively couple first computer system 102 and server104 to network 116, may include any number of communication links,including wired and/or wireless links, such as IEEE 802.11 wireless LAN(WLAN) wireless links, Worldwide Interoperability for Microwave Access(Wi-MAX) links, cellular network links, wireless personal area network(PAN) links (e.g., Bluetooth™ links), Ethernet links, USB links, etc.

System 100 is configured to enable computer systems such as computersystem 102 to issue queries to database 114. For example, as shown inFIG. 1, computer system 102 includes a query source 106. Query source106 may be an application or other entity that is configured to issuequeries. Furthermore, as shown in FIG. 1, server 104 includes a queryprocessing system 108. Query processing system 108 is configured toreceive and issue queries against database 114.

For instance, query source 106 may generate a query 110. Query 110 maybe a parameterized query having values provided for a plurality ofparameters, for example. Query 110 may be formatted according to anyquery language, including SQL. Query 110 is transmitted from computersystem 102 in a first communication signal through first communicationlink 122, network 116, and second communication link 124, to server 104.The first communication signal may be transmitted in any form. Server104 receives query 110 in the first communication signal.

Query processing system 108 processes query 110. Query processing system108 processes query 110 to generate a query plan, issues the query planfor query 110 against database 114 to generate response data, andincludes the response data in a query response 112. In an embodiment,query response 112 may include one or more records and/or other data ofdatabase 114. Server 104 generates a second communication signal thatincludes query response 112. The second communication signal istransmitted through second communication link 124, network 116, andfirst communication link 122, and is received by computer system 102.The second communication signal may be transmitted in any form. Querysource 106 at computer system 102 receives query response 112, and mayutilize the response data of query response 112 in any manner.

Query processing system 108 may be configured in various ways to processquery 110. For example, FIG. 2 shows a block diagram of query processingsystem 108, according to an example embodiment. As shown in FIG. 2,query processing system 108 includes a query interface 202, a dispatchergenerator 204, a query compiler 206, a dispatcher 208, and a queryexecution module 210. These elements of query processing system 108 aredescribed as follows.

Query interface 202 is optionally present. When present, query interface202 may be configured to receive queries such as query 110 of FIG. 1,and may be configured to extract query predicates from each query.

Dispatcher generator 204 is optionally present. When present, dispatchergenerator 204 is configured to generate dispatcher 208, which is used togenerate and manage query plans (which may also be referred to as“execution plans,” etc.) for queries of a corresponding query template.A query plan is a tree of operations (query operators) that a queryengine performs to execute a query on a database. Such operations arewell known to persons skilled in the relevant art(s). Examples of queryoperators include “Join,” “GroupBy,” “OrderBy,” etc. In an embodiment,the first time during a runtime that a query is received according to aparticular query template, dispatcher 208 may not be present. Dispatchergenerator 204 may determine that dispatcher 208 is not present for thequery template, and therefore may generate dispatcher 208 for thatparticular query template. In an embodiment, dispatcher generator 204may compile the first received query to generate dispatcher 208.

Dispatcher 208 is configured to manage query plans for queries receivedaccording to its associated query template. For example, dispatcher 208may be configured to look up a query plan for a received query, torequest that a query plan be generated for a query if a suitable queryplan for the query is not present, and to provide the looked-up orgenerated query plan for execution on the query. Dispatcher 208 storesquery plans as separate entries (e.g., in a cache, as described below),and assigns keys to the stored query plans. In this manner, the queryplans can be separately considered by eviction policy or executionstatistics collection. Furthermore, as described in further detailbelow, dispatcher 208 may associate one or more selectivity regions in aselectivity space to each query plan. As such, dispatcher 208 may bereferred to as a “selectivity region mapper.” The use of selectivityregions enables dispatcher 208 to manage query plans in an efficientmanner, enabling a balance to be maintained between managing a smallnumber of more general query plans versus managing a larger number ofmore specific query plans.

Query compiler 206 is configured to compile query plans for receivedqueries. For instance, query compiler 206 may be configured to compilequery plans for queries at the request of dispatcher 208. Query compiler206 may be configured to compile queries in any manner, including usingconventional techniques, as would be known to persons skilled in therelevant art(s).

Query execution module 210 is configured to execute query plans selectedby dispatcher 208 on received queries. For example, query executionmodule 210 may interact with database 114 of FIG. 1 to retrieve data forquery response 112 by executing the selected query plan on query 110.Query execution module 210 may be configured to execute queries based onquery plans in any manner, including using conventional techniques, aswould be known to persons skilled in the relevant art(s).

Example embodiments are described in the following subsections forprocessing received queries, generating keys, determining selectivityregions, and managing query plans.

A. Example Embodiments for Processing a Received Query

Embodiments for processing queries may be performed in various ways. Forinstance, FIG. 3 shows a flowchart 300 for processing queries, accordingto an example embodiment. In an embodiment, query processing system 108may operate according to flowchart 300. Further structural andoperational embodiments will be apparent to persons skilled in therelevant art(s) based on the discussion regarding flowchart 300.Flowchart 300 is described as follows. For illustrative purposes,flowchart 300 is described with respect to FIG. 4, which shows queryprocessing system 108 of FIG. 2 processing a received query, accordingto an example embodiment.

In step 302, a first query configured according to a query template isreceived, the first query including values for a plurality of parametersof the query template. For example, as shown in FIG. 4, query interface202 receives query 110. When present, query interface 202 may beconfigured to identify query predicates in query 110 (e.g., tofacilitate generation of query compilation plans), and may outputreceived query 412 (which may include the identified query predicates,etc.). In general, efficiency expressions are parameterizedsubexpressions of query predicates referencing columns from a singletable. Techniques for identifying efficiency expressions of a query areknown to persons skilled in the relevant art(s).

Query 110 may be a query that is configured according to a particularquery template. For instance, FIG. 5 shows a block diagram of a querytemplate 500, according to an example embodiment. As shown in FIG. 5,query template 500 includes a plurality of efficiency expressions 502a-502 n. Efficiency expressions 502 are parameterized subexpressions ofquery predicates referencing columns from a single table. Any number ofefficiency expressions 502 may be included in query template 500. Eachefficiency expression 502 may be an expression (e.g., a Booleanexpression). An efficiency expression 502 is “parameter-sensitive” ifthe subexpression depends on a supplied parameter value. For example, asshown in FIG. 5, each of efficiency expressions 502 a-502 n includes acorresponding one of parameters 504 a-504 n. Parameters 504 a-504 n maybe unspecified until runtime (e.g., may be indicated by symbols orplaceholders in query template 500), when parameter values may beprovided for each of parameters 504 a-504 n in an issued query. Querytemplate 500 may be configured (e.g., by a user or developer) atdevelopment time for query source 106 of FIG. 1, for example. Query 110includes values for each of efficiency expressions 502 a-502 n so thatquery 110 may be executed at runtime.

For illustrative purposes, a query template is shown below as an exampleof query template 500:SELECT*FROM T WHERE (a=@p1) OR (b<@p2)As shown above, the query template includes first and second efficiencyexpressions, (a=@p1) and (b<@p2). This query template is configured toselect rows of a table T where a column “a” of the table T has an entryequal to “@p1” or where a column “b” of the table T has an entry that isless than “@p2”, the values for the parameters “@p1” and “@p2” beingfilled in at runtime. For example, query 110 may include a parametervalues of “5” for the parameter “@p1” and “32” for the parameter “@p2”,such that query 110 is SELECT*FROM T WHERE (a=5) OR (b<32).

In step 304, a dispatcher is determined to not be present for the firstquery. For example, in an embodiment, dispatcher generator 204 receivesreceived query 412, and determines whether a dispatcher is present forreceived query 412 and/or for the query template associated withreceived query 412. If a dispatcher is determined to not be present,step 306 may be performed. If dispatcher 208 is determined to bepresent, step 306 does not need to be performed.

In step 306, a dispatcher is generated for the query template inresponse to the dispatcher being determined to not be present. Forexample, in an embodiment, dispatcher generator 204 is configured togenerate dispatcher 208 if dispatcher 208 is not present upon receivingreceived query 412. As shown in FIG. 4, dispatcher generator 204generates dispatcher 208 after receiving received query 412. Dispatcher208 is stored in cache 402 included in query processing system 108.Cache 402 may be included in one or more storage devices, such as one ormore memory devices. Dispatcher generator 204 may generate dispatcher208 in various ways. For example, dispatcher generator 204 may identifythe efficiency expressions included in a received parameterized query,or may receive the identified efficiency expressions from queryinterface 202, as described above. For instance, with regard to theexample query template shown above, the efficiency expressions of (a=5)and (b<32) may be identified, and stored in dispatcher 208.

Dispatcher 208 stores compile time structures required for evaluatingefficiency expressions. For example, dispatcher 208 may store statisticsdescribing data distribution in tables referenced in the query template.FIG. 6 shows a block diagram of dispatcher 208, according to an exampleembodiment. As shown in FIG. 6, dispatcher 208 includes efficiencyexpressions 608. Efficiency expressions 608 include the identifiedefficiency expressions of received query 412. Furthermore, as shown inFIG. 6, dispatcher 208 includes a key generator 602, a query planselector 604, a query compiler caller 606, and a selectivity regionmapper 610. These elements of dispatcher 208 are described as follows.

Key generator 602 is configured to generate keys for query plansassociated with dispatcher 208, as described in further detail below.For example, the generated keys may be used to identify and/ordistinguish different query plans. Query plan selector 604 is configuredto lookup stored query plans for received queries associated withdispatcher 208. For instance, query plan selector 604 may look up storedquery plans using the keys generated by key generator 602. Querycompiler caller 606 is configured to request compilation of a query togenerate a query plan when a query plan is not present for the query, asdetermined by query plan selector 604. Selectivity region mapper 610 isconfigured to map queries to regions of a selectivity space, asdescribed in further detail below.

Note that steps 304 and 306 are optional, and in other embodiments,dispatcher 208 may be already present (e.g., in storage) and/or may begenerated prior to receiving queries.

In step 308, a selectivity space is defined as an array of selectivityregions arranged according to a grid having a number of dimensions equalto a number of efficiency expressions of the query template. In anembodiment, a selectivity space is defined that is used to manage queryplans. The selectivity space is configured to have a number ofdimensions equal to the number of efficiency expressions of the queries.The selectivity space is arranged according to a logarithmic-scale grid,with each region of the grid (selectivity region) corresponding to apercentage or fraction of rows of the table(s) targeted by the queries.

For instance, FIG. 7 shows an example graphical representation of aselectivity space 700, according to an embodiment. Selectivity space 700is a two-dimensional selectivity space, and thus corresponds to a querytemplate having two efficiency expressions. If selectivity space 700corresponded to a query template having three or more efficiencyexpressions, selectivity space 700 would have three or more dimensionscorresponding to the number of efficiency expressions. However,selectivity space 700 is provided as a two-dimensional selectivity spacefor ease of illustration.

As shown in FIG. 7, selectivity space 700 has a first axis 702 labeledas “S1” and a second axis 704 labeled as “S2”. “S1” and “S2” are“selectivities” that correspond to a pair of efficiency expressions. Forinstance, referring to the example query template shown above(SELECT*FROM T WHERE (a=@p1) OR (b<@p2)), “S1” may correspond to thefirst efficiency expression (a=@p1), and “S2” may correspond to thesecond efficiency expression (b<@p2).

As shown in FIG. 7, first axis 702 and second axis 702 have logarithmicscales, each starting with zero at the origin and having a maximum of1.0, with a progression from 0 to 1.0 according to logarithmic factorsof 10, of 0.0001, 0.001, 0.01, and 0.1. Thus, a five-by-five grid isformed by selectivity space 700, forming twenty-five selectivity regions706 a-706 y (or “selectivity cells”). In other embodiments, aselectivity space may form a grid of a different number of selectivityregions than shown in FIG. 7 (e.g., a 10 by 10 grid, a 7 by 7 grid, a100 by 100 grid, etc.).

Each selectivity region 706 corresponds to a range of fractions of thetotal number of rows of the targeted table that satisfy the efficiencyexpressions associated with the axes. For instance, in FIG. 7, withrespect to the example query template shown above (SELECT*FROM T WHERE(a=@p1) OR (b<@p2)), selectivity region 706 a at coordinates (0, 4)corresponds to the fractional range of 0-0.0001 of the total number ofrows of the targeted table that satisfy (a=@p1) for particular valuesfor the parameter @p1, and the fractional range of 0.1-1.0 of the totalnumber of rows of the targeted table that satisfy (b<@p2) for particularvalues for the parameter @p2, as determined by dispatcher 208.Selectivity region 706 i at coordinate (3, 3) corresponds to thefractional range of 0.01-0.1 of the total number of rows of the targetedtable that satisfy (a=@p1) for particular values for the parameter @p1,and the fractional range of 0.01-0.1 of the total number of rows of thetargeted table that satisfy (b<@p2) for a particular values for theparameter @p2, as determined by dispatcher 208.

A plurality of query points 708 are shown indicated in selectivity space700 that each corresponds to a received query (after such queries arereceived). Each query point 708 corresponds to selectivities determinedfor the corresponding query—a fraction of the total number of rowsqualified by the corresponding efficiency expressions for particularparameter values of the query. For instance, query point 708 a isindicated as located in selectivity region 706 i. Query point 708 a maycorrespond to a particular received query providing (5, 32) as valuesfor the parameters @p1 and @p2 for the above example query template. Insuch an example, that particular received query resulted in the fractionof 0.5 of the total number of rows of the targeted table that satisfy(a=5) and resulted in the fraction of 0.4 of the total number of rows ofthe targeted table that satisfy (b<32).

Query plans have different efficiencies depending on parameter values. Aquery plan may be assumed to remain efficient for a set of queries aslong as the parameter values of the queries lead to respectively similarsizes of intermediate results over single tables. As such, eachselectivity region 706 of selectivity space 700 may correspond to aparticular query plan. The query plan for a particular selectivityregion 706 is assumed to remain sufficiently efficient for a set ofqueries (graphically represented by the query points 708 located in theselectivity region 706 in FIG. 7) having parameter values such thattheir selectivities reside therein.

It is noted that dispatcher 208 and associated plan variants 404 may beinvalidated when statistics describing data distribution in tablesreferenced in the respective query template are refreshed. This isbecause cardinality estimates may be different after the tables arerefreshed, if the data is significantly different. In such a case,dispatcher 208 may need to be regenerated.

Referring back to flowchart 300 in FIG. 3, in step 310, the first queryis compiled to generate a first query plan. Query plan selector 604 mayuse the key generated for received query 412 to attempt to look up aquery plan for query 412 that is stored in cache 402. Query planselector 604 may not find a query plan associated with the generatedkey, and thus may determine that no query plan for query 412 is present.As such, query compiler 206 may transmit a query compile request 414 toquery compiler 206 to request that a query plan for received query 412be generated. Query compiler 206 receives query compile request 414, andcompiles received query 412 to generate a query plan for received query412.

In step 312, a first selectivity region in the selectivity space isdetermined for the first query. For example, in an embodiment,selectivity region mapper 610 may determine a selectivity region of theselectivity space associated with the first query. As shown in FIG. 4,dispatcher 208 receives received query 412. Selectivity region mapper610 is configured to map the query plan generated for received query 412to a selectivity region in the selectivity space defined according tostep 308.

Selection of query plans by a query optimizer depends on selectivitiesof subtrees of an input relational tree for a query. Selectivities Si ofa selectivity space may be defined as shown below for Equation 1:Si=CARD(Ei)/CARD(Ti),  Equation 1where

i=a number of selectivities (efficiency expressions) of the receivedquery,

Si=a selectivity that corresponds to a efficiency expression of thequery,

Ei=an efficiency expression of selectivity i, and

Ti=a table targeted by the efficiency expression i.

Equation 1 expresses a mapping between regions in the space of theseselectivities to plans (e.g., as shown graphically in FIG. 7). Similarselectivities tend to lead to similar query plans, so that a same queryplan may be optimal or at least efficient in a given neighborhood. Dueto a high cost of query compilation and representation cost, a mappingbetween query plans and selectivity regions is approximated with certaingranularity and may be built lazily. As described above, a grid isdefined over the selectivity space, such that the axes of theselectivity regions follow a logarithmic scale, in an embodiment. Eachselectivity region may be associated with a query plan. When a querywith parameter values (pi, Ti) is submitted for execution, selectivityregion mapper 610 may generate selectivities si(pi,Ti) for the query todetermine the selectivity region for the query.

Selectivity region mapper 610 may generate an indication of theselectivity region in the form of coordinates in the selectivity spacefor the selectivity region. For example, in selectivity space 700 ofFIG. 7, received query 412 may correspond to point 708 a residing inselectivity region 706 i. As such, selectivity region mapper 610 maygenerate an indication of selectivity region 706 i as the coordinates(3, 3), for example. In other embodiments, selectivity region mapper 610may generate an indication of a selectivity region in another form.

Referring back to FIG. 3, in step 314, a first key is generated for thefirst query that includes an indication of the first selectivity region.Key generator 602 of dispatcher 208 may generate a key for receivedquery 412. The generated key is configured to be used to match a queryplan with received query 412. Furthermore, the generated key isconfigured to express an applicability of the matched query plan (e.g.,applicability to further queries). In an embodiment, key generator 602forms a key 800 for each received query as shown in FIG. 8. As shown inFIG. 8, key 800 includes query description 802 and SR indication 804.Query description 802 may be the query template (e.g., in the form ofquery language text, such as SQL) associated with received query 412,formed by entering placeholders for the parameter values of receivedquery 412. SR indication 804 is an indication of the selectivity regiongenerated by selectivity region mapper 610 for received query 412 instep 312. In alternative embodiments, key 800 may have otherconfigurations.

In step 316, the first query plan is stored in association with thefirst key. For example, as shown in FIG. 4, dispatcher 208 outputs a key406, which is the key generated for received query 412 (e.g., in step314, by key generator 602), and key 406 is stored in cache 402. Querycompiler 206 outputs a query plan 410, which is the query plan generatedfor received query 412 (e.g., in step 310). Query plan 410 is stored incache 402 (e.g., by query compiler 206 or by dispatcher 208) inassociation with key 406. The entry of key 406 and query plan 410 incache 402 may be referred to as a plan variant 404. Any number of planvariants 404 may be stored in cache 402 that each include acorresponding key 406 and query plan 410.

In step 318, the first query plan is executed with respect to the firstquery. For example, as shown in FIG. 4, dispatcher 208 transmits a queryexecution request 416, which is received by query execution module 210.Query execution module 210 also receives received query 412. Queryexecution request 416 may include query plan 410, and is a request forquery execution module 210 to execute query plan 410 with respect toreceived query 412. For example, query execution module 208 may interactwith database 114 of FIG. 1 to retrieve data for query response 412 byexecuting query plan 410 on query 412. As shown in FIG. 4, queryexecution module 210 transmits query response 112, which may include oneor more records and/or other data of database 114. As shown in FIG. 1,query response 112 may be received by query source 106.

B. Example Embodiments for Processing Subsequent Queries

Queries that are received by query processing system 108 subsequently tofirst query 110 may be processed in a similar manner. For instance, FIG.9 shows a flowchart 900 for processing subsequent queries, according toan example embodiment. For example, in an embodiment, query processingsystem 108 may operate according to flowchart 900. Further structuraland operational embodiments will be apparent to persons skilled in therelevant art(s) based on the discussion regarding flowchart 900.Flowchart 900 is described as follows.

In step 902, a second query configured according to the query templateis received, the second query including values for the plurality ofparameters. For example, referring to FIG. 4, a second query similar toquery 110 may be received by query interface 202. The second query maybe the next query received after query 110 or may be any subsequentlyreceived query, and may include a second set of values for the pluralityof parameters. The second query may be configured according to the samequery template as query 110, and thus may be handled by dispatcher 208.Note that in an embodiment, upon receipt of each additional query,dispatcher generator 204 may determine whether a dispatcher is presentfor the query (similarly to step 304 of flowchart 300 in FIG. 3) priorto proceeding to step 904 of flowchart 900.

In step 904, a second selectivity region in the selectivity space isdetermined for the second query. For instance, selectivity region mapper610 of FIG. 6 may map the second query to a selectivity region in theselectivity space (similarly to step 312 described above). Selectivityregion mapper 610 may generate an indication of the selectivity regionin the form of coordinates in the selectivity space for the selectivityregion, or in other form. For example, in selectivity space 700 of FIG.7, the second query may correspond to point 708 c residing inselectivity region 706 n. As such, selectivity region mapper 610 maygenerate an indication of selectivity region 706 i as the coordinates(3, 2), for example.

In step 906, a second key is generated for the second query thatindicates the second selectivity region. For example, in an embodiment,key generator 602 of FIG. 6 may generate a key for the second query thatincludes query description 802 of FIG. 8 (e.g., text of the querytemplate) and an indication of the second selectivity region determinedin step 904 for the second query.

For instance, FIG. 10 shows a block diagram of cache 402 of FIG. 4,according to an example embodiment. As shown in FIG. 10, cache 402includes dispatcher 208 of FIG. 6 and a plurality of plan variants 404a-404 n. Plan variants 404 a-404 n each include a corresponding one ofeach of keys 406 a-406 n and query plans 410 a-410 n. As shown in FIG.10, selectivity region mapper 610 of dispatcher 208 receives a query1002, which may be a query received subsequently to query 110.Selectivity region mapper 610 generates a selectivity region indication1004, which is received by key generator 602. Key generator 602generates a key 1004 for query 1002, in a manner as described above.

In step 908, whether the second key matches any stored key is determinedFor example, as shown in FIG. 10, query plan selector 604 may receivekey 1004. Query plan selector 604 may use key 1004 to attempt to look upa query plan for query 1002 that is stored in cache 402, as indicated bydotted line 1012 in FIG. 10.

If query plan selector 604 determines that one of keys 406 a-406 nmatches key 1004, step 1102 shown in FIG. 11 may be performed. In step1102, the looked up query plan is executed with respect to the secondquery. For instance, if key 406 a in cache 402 matches key 1004, queryplan 410 a may be used to execute query 1002. As such, query planselector 604 may generate a query execution request 1010, which isreceived by query execution module 210 (query execution module 210 alsoreceives query 1002). Query execution request 416 includes query plan410, and is a request for query execution module 210 to execute queryplan 410 a with respect to query 1002.

If query plan selector 604 determines that none of keys 406 a-406 nmatches key 1004, flowchart 1200 shown in FIG. 12 may be performed,which is similar to steps 310, 316, and 318 of flowchart 300 in FIG. 3.In step 1202 of flowchart 1200, the second query is compiled to generatea second query plan. Furthermore, as shown in FIG. 10, query planselector 604 generates a need query plan indication 1006. Query compilercaller 606 receives need query plan indication 1006, and transmits aquery compile request 1008 to query compiler 206 to request that a queryplan be generated for query 1002. Query compiler 206 receives querycompile request 1008, and compiles query 1002 to generate a query planfor query 1002.

In step 1204, the second query plan is stored in association with thesecond key. For example, key 1004 generated by key generator 602 and thequery plan generated by query compiler 206 for query 1002 may be storedin cache 402 as an additional plan variant 404 (e.g., plan variant 404o).

In step 1206, the second query plan is executed with respect to thesecond query. For example, as described above with respect to FIG. 4,dispatcher 208 may transmit a query execution request that is receivedby query execution module 210, that is a request for query executionmodule 210 to execute the query plan generated in step 1202 with respectto query 1002. Query execution module 208 may interact with database 114of FIG. 1 to retrieve data for a query response by executing the queryplan on query 1002. As described above with respect to FIG. 4, queryexecution module 210 may transmit a query response that may include oneor more records and/or other data of database 114.

C. Example Embodiments for Managing Plan Variants

The plan variants generated and used for received queries may be managedin various ways. In some embodiments, plan sharing is performed, whilein other embodiments, plans are not shared. For instance, in oneembodiment, each plan variant 404 stored in cache 402 may be maintainedseparately. In other words, regardless of whether two or more queryplans 410 stored in cache 402 are identical, in an embodiment, noassociation is made between the corresponding plan variants 404. In sucha case, no plan sharing is performed. In an embodiment where plansharing is not performed, a key 406 may include an indication of anassociated selectivity region (selectivity region indication 804), asdescribed above.

In another embodiment, plan variants 404 stored in cache 402 may bemerged together if their corresponding query plans 410 are substantiallythe same (e.g., the query plans have the same tree structure). In such acase, plan sharing is performed. In an embodiment where plan sharing isperformed, a key 406 may not be configured to include selectivity regionindication 804. Instead, key 406 may include an identifier (e.g., anumber) for a query plan signature, and dispatcher 208 may include amapping between selectivity regions and plan signatures. This way asingle query plan may be associated with one or more selectivityregions.

FIG. 13 shows a flowchart for managing selectivity regions using plansharing, according to an example embodiment. Further structural andoperational embodiments will be apparent to persons skilled in therelevant art(s) based on the discussion regarding flowchart 1300.Flowchart 1300 is described as follows.

In step 1302, whether the second query plan substantially matches thefirst query plan is determined. For example, dispatcher 208 may comparea query plan generated by query compiler 206 (e.g., in step 1202 of FIG.12) to the first query plan (e.g., generated in step 310 of FIG. 3) andany other query plans (e.g., query plans 410 a-410 n) stored in cache402 to determine whether the generated query plan matches any of them.Query plans may match if they are substantially the same. For example, aquery plan has a particular operator tree structure, as would be knownto persons skilled in the relevant art(s). In an embodiment, a queryplan structure may be expressed by a number referred to as a plansignature. Whether a pair of query plans substantially match may bedetermined by comparing plan signatures of the query plans (i.e.,comparing the numbers that represent the plan signatures).

In step 1304, the second query plan is stored in association with thesecond key if the second query plan is determined to not match the firstquery plan. For example, if the generated query plan is determined tonot substantially match any query plan stored in cache 402, the key forthe query plan and the query plan may be stored as a separate planvariant 404 in cache 402 (e.g., as described above in step 1204).

In step 1306, an indication of the second selectivity region is storedin association with the first query plan and the first key if the secondquery plan is determined to match the first query plan. For example, inan embodiment, if the generated query plan is determined tosubstantially match a query plan stored in cache 402, rather thanperforming step 1206, the indication of the selectivity regiondetermined for the query plan may be stored in the plan variant of thematching query plan in cache 402. For example, if the generated queryplan is determined to substantially match first query plan 410 a (e.g.,having SR indication 408 a of (3, 3)), and the selectivity region forthe generated query plan was determined to be (3, 2), the SR indicationof (3, 2) may be included with the existing SR indication 408 a of planvariant 404 a. As such, SR indication 408 a of plan variant 404 a may be(3, 2), (3, 3). In such an embodiment, no separate plan variant 404 needbe added to cache 402 by dispatcher 208 for the generated query plan.

II. Further Example Embodiments

It is noted that in the example embodiments described above, parametersare typically referenced as regular parameters (e.g., single values). Inembodiments, the parameters described above may alternatively betable-valued parameters. For instance, in addition to scalar parametersthat are placeholders for simple types (e.g., numbers, strings),table-valued parameter (TVPs) may be used, where a TVP refers to atable. The table used for a TVP is provided at runtime. For instance, anexample query template is shown below:SELECT name from @STUDENTS where @STUDENTS.grade=‘A’,where values of @STUDENTS are different tables. This type of query canbe processed by the same techniques described herein for scalarparameters. In the above example, @STUDENTS.grade=‘A’ is an efficiencyexpression. Selectivity region mapper 610 may be configured to computecardinality for this efficiency expression. At runtime, for regularparameters, parameter values are used to evaluate efficiencyexpressions. In the case of TVPs, as in the present example, statisticsdescribing data distribution in the provided table are used.

In embodiments, new plan variants and mappings to efficiency regions aredetermined as subsequent queries are received and processed. However,for equality predicates, such as a=@p, it is possible to predetermineplan variants before receiving actual queries from a workload (e.g.,prior to runtime). Query plans may be generated for characteristicvalues based on the data distribution of a column represented by columnstatistics, such as a histogram. For example, in an embodiment, a planvariant may be pre-generated for a frequent value of “a”, such as “USA.”Furthermore, a plan variant may be pre-generated for an infrequent valueof “a”, such as “FIJI.”

In an embodiment, selectivity region mapper 610 may use the samecardinality estimation logic as query compiler 206 to map a query to aselectivity region. As such, the values obtained during mapping andquery compilation are consistent. In addition, selectivity region mapper610 may be configured to handle general predicates.

In an embodiment, it may be desirable for query processing system 108 tobe independent of an order of incoming queries (e.g., the efficiencyregion mapping and plan variant generation may be performed for a givenset of queries regardless of their received order). In such anembodiment, a query may not be compiled for supplied parameter values toobtain a plan variant for a given selectivity region, and instead, asame point in the selectivity region, (e.g., upper right corner) may beselected, and the query may be compiled, to force selectivitiesassociated with that point on respective efficiency expressions. Assuch, query compiler 206 may be configured to generate query plans underselectivity constraints on query subexpressions.

In an embodiment, selectivity region mapper 610 may be configured toevaluate efficiency expressions before running any query. In such anembodiment, cardinality estimation logic may be executed that is costly,especially, when the query itself runs fast. As such, simple perdimension caches stored in the dispatcher may be used. Each cache maymap parameter values involved in the efficiency expression associatedwith a given dimension to a relevant cardinality estimate. It is notedthat users may tend to frequently reuse the same parameter values, andin such cases, the cache may allow the cardinality estimation logic tobe bypassed, and for the correct estimate to be immediately output.

III Example Computing Device Embodiments

Query processing system 108, query interface 202, dispatcher generator204, query compiler 206, dispatcher 208, query execution module 210, keygenerator 602, query plan selector 604, query compiler caller 606, andselectivity region mapper 610 may be implemented in hardware, software,firmware, or any combination thereof. For example, query processingsystem 108, query interface 202, dispatcher generator 204, querycompiler 206, dispatcher 208, query execution module 210, key generator602, query plan selector 604, query compiler caller 606, and/orselectivity region mapper 610 may be implemented as computer programcode configured to be executed in one or more processors. Alternatively,query processing system 108, query interface 202, dispatcher generator204, query compiler 206, dispatcher 208, query execution module 210, keygenerator 602, query plan selector 604, query compiler caller 606,and/or selectivity region mapper 610 may be implemented as hardwarelogic/electrical circuitry.

FIG. 16 depicts an exemplary implementation of a computer 1600 in whichembodiments of the present invention may be implemented. For example,computer system 102 and/or server 104 may each be implemented similarlyto computer 1600, including one or more features of computer 1600 and/oralternative features. Computer 1600 may be a general-purpose computingdevice in the form of a conventional personal computer, a mobilecomputer, or a workstation, for example, or computer 1600 may be aspecial purpose computing device. The description of computer 1600provided herein is provided for purposes of illustration, and is notintended to be limiting. Embodiments of the present invention may beimplemented in further types of computer systems, as would be known topersons skilled in the relevant art(s).

As shown in FIG. 14, computer 1400 includes a processing unit 1402, asystem memory 1404, and a bus 1406 that couples various systemcomponents including system memory 1404 to processing unit 1402. Bus1406 represents one or more of any of several types of bus structures,including a memory bus or memory controller, a peripheral bus, anaccelerated graphics port, and a processor or local bus using any of avariety of bus architectures. System memory 1404 includes read onlymemory (ROM) 1408 and random access memory (RAM) 1410. A basicinput/output system 1412 (BIOS) is stored in ROM 1408.

Computer 1400 also has one or more of the following drives: a hard diskdrive 1414 for reading from and writing to a hard disk, a magnetic diskdrive 1416 for reading from or writing to a removable magnetic disk1418, and an optical disk drive 1420 for reading from or writing to aremovable optical disk 1422 such as a CD ROM, DVD ROM, or other opticalmedia. Hard disk drive 1414, magnetic disk drive 1416, and optical diskdrive 1420 are connected to bus 1406 by a hard disk drive interface1424, a magnetic disk drive interface 1426, and an optical driveinterface 1428, respectively. The drives and their associatedcomputer-readable media provide nonvolatile storage of computer-readableinstructions, data structures, program modules and other data for thecomputer. Although a hard disk, a removable magnetic disk and aremovable optical disk are described, other types of computer-readablemedia can be used to store data, such as flash memory cards, digitalvideo disks, random access memories (RAMs), read only memories (ROM),and the like.

A number of program modules may be stored on the hard disk, magneticdisk, optical disk, ROM, or RAM. These programs include an operatingsystem 1430, one or more application programs 1432, other programmodules 1434, and program data 1436. Application programs 1432 orprogram modules 1434 may include, for example, computer program logicfor implementing query processing system 108, query interface 202,dispatcher generator 204, query compiler 206, dispatcher 208, queryexecution module 210, key generator 602, query plan selector 604, querycompiler caller 606, selectivity region mapper 610, flowchart 300,flowchart 900, step 1102, flowchart 1200 and/or flowchart 1300(including any step of flowcharts 300, 900, 1200, and 1300).

A user may enter commands and information into the computer 1400 throughinput devices such as keyboard 1438 and pointing device 1440. Otherinput devices (not shown) may include a microphone, joystick, game pad,satellite dish, scanner, or the like. These and other input devices areoften connected to the processing unit 1402 through a serial portinterface 1442 that is coupled to bus 1406, but may be connected byother interfaces, such as a parallel port, game port, or a universalserial bus (USB).

A display device 1444 is also connected to bus 1406 via an interface,such as a video adapter 1446. In addition to the monitor, computer 1400may include other peripheral output devices (not shown) such as speakersand printers.

Computer 1400 is connected to a network 1448 (e.g., the Internet)through an adaptor or network interface 1450, a modem 1452, or othermeans for establishing communications over the network. Modem 1452,which may be internal or external, is connected to bus 1406 via serialport interface 1442.

As used herein, the terms “computer program medium” and“computer-readable medium” are used to generally refer to media such asthe hard disk associated with hard disk drive 1414, removable magneticdisk 1418, removable optical disk 1422, as well as other media such asflash memory cards, digital video disks, random access memories (RAMs),read only memories (ROM), and the like.

As noted above, computer programs and modules (including applicationprograms 1432 and other program modules 1434) may be stored on the harddisk, magnetic disk, optical disk, ROM, or RAM. Such computer programsmay also be received via network interface 1450 or serial port interface1442. Such computer programs, when executed or loaded by an application,enable computer 1400 to implement features of embodiments of the presentinvention discussed herein. Accordingly, such computer programsrepresent controllers of the computer 1400.

The invention is also directed to computer program products comprisingsoftware stored on any computer useable medium. Such software, whenexecuted in one or more data processing devices, causes a dataprocessing device(s) to operate as described herein. Embodiments of thepresent invention employ any computer-useable or computer-readablemedium, known now or in the future. Examples of computer-readablemediums include, but are not limited to storage devices such as RAM,hard drives, floppy disks, CD ROMs, DVD ROMs, zip disks, tapes, magneticstorage devices, optical storage devices, MEMs, nanotechnology-basedstorage devices, and the like.

VI. Conclusion

While various embodiments of the present invention have been describedabove, it should be understood that they have been presented by way ofexample only, and not limitation. It will be understood by those skilledin the relevant art(s) that various changes in form and details may bemade therein without departing from the spirit and scope of theinvention as defined in the appended claims. Accordingly, the breadthand scope of the present invention should not be limited by any of theabove-described exemplary embodiments, but should be defined only inaccordance with the following claims and their equivalents.

What is claimed is:
 1. A method, comprising: receiving a first queryconfigured according to a query template, the first query includingvalues for a plurality of parameters of the query template, the firstquery comprising a plurality of efficiency expressions; managing aplurality of query plans using a selectivity space by: defining theselectivity space as an array of selectivity regions arranged accordingto a grid that has a number of dimensions equal to a number ofefficiency expressions of the first query, the grid, in accordance withthe plurality of efficiency expressions, comprising a multi-dimensionallogarithmic-scale grid divided into a plurality of multi-dimensionalregions forming the array of selectivity regions, compiling, the firstquery to generate a first query plan, determining a first selectivityregion in the selectivity space for the first query, generating a firstkey for the first query that includes an indication of the firstselectivity region, and storing, by a computer system, the first queryplan in association with the first key; and executing, by the computersystem, the first query plan with respect to the first query.
 2. Themethod of claim 1, further comprising: determining, by the computersystem, a plan variant for an equality efficiency expression of thequery template prior to receiving the first query, the plan variantincluding a corresponding key and query plan; and storing, by thecomputer system, the plan variant.
 3. The method of claim 1, furthercomprising: evaluating, by the computer system, the efficiencyexpressions prior to receiving the first query, including mappingparameter values of an efficiency expression associated with a givendimension to a relevant cardinality estimate.
 4. The method of claim 1,further comprising: receiving, by the computer system, a second queryconfigured according to the query template, the second query includingvalues for the plurality of parameters; determining, by the computersystem, a second selectivity region in the selectivity space for thesecond query; generating, by the computer system, a second key for thesecond query that includes the second selectivity region; determiningwhether the second key matches any stored key including the first key;and executing, by the computer system, the first query plan with respectto the second query if the second key is determined to match the firstkey.
 5. The method of claim 4, further comprising: if the second key isdetermined to not match any stored key, compiling, by the computersystem, the second query to generate a second query plan, and executing,by the computer system, the second query plan with respect to the secondquery.
 6. The method of claim 5, further comprising: storing, by thecomputer system, the second query plan in association with the secondkey if the second key is determined to not match any stored key.
 7. Themethod of claim 5, further comprising: determining, by the computersystem, whether the second query plan substantially matches the firstquery plan; storing, by the computer system, the second query plan inassociation with the second key if the second query plan is determinedto not match the first query plan; and storing, by the computer system,an indication of the second selectivity region in association with thefirst query plan and the first key if the second query plan isdetermined to match the first query plan.
 8. The method of claim 1,wherein at least one parameter of the plurality of parameters is a tablevalued parameter.
 9. The method of claim 1, further comprising:determining, by a computer system, that a dispatcher is not present forthe query template; and generating, by the computer system, a dispatcherfor the query template in response to said determining that a dispatcheris not present for the query template; wherein said compiling comprisescompiling, using the generated dispatcher, the first query to generatethe first query plan; wherein said determining a first selectivityregion comprises determining the first selectivity region in theselectivity space for the first query using the generated dispatcher;and wherein said generating a first key comprises generating, using thegenerated dispatcher, the first key for the first query that includesthe indication of the first selectivity region.
 10. The method of claim1, wherein the grid is a logarithmic-scale grid, and said definingcomprises: defining the selectivity space as the array of selectivityregions arranged according to the logarithmic-scale grid that has thenumber of dimensions equal to the number of efficiency expressions ofthe first query.
 11. A system, comprising: a query interface configuredto receive queries configured according to a query template, the queryinterface receiving a first query that includes values for a pluralityof parameters of the query template, the first query comprising aplurality of efficiency expressions; a query plan manager that manages aplurality of query plans using a selectivity space. the query planmanager comprising: a selectivity region mapper being configured to mapthe first query to a first selectivity region in the selectivity spaceusing at least one processor, the selectivity space being an array ofselectivity regions arranged according to a grid having a number ofdimensions equal to a number of efficiency expressions of the firstquery, the grid, in accordance with the plurality of efficiencyexpressions, comprising a multi-dimensional grid divided into aplurality of multi-dimensional regions forming the array of selectivityregions; a key generator being configured to generate a first key usingthe at least one processor for the first query that includes anindication of the first selectivity region; a query compiler callerbeing configured to request the first query to be compiled to generate afirst query plan; the first query plan being stored in association withthe first key; and the first query plan being executed with respect tothe first query.
 12. The system of claim 11, wherein the key generatoris configured to form the first key to include the query template andthe indication of the first selectivity region.
 13. The system of claim11, further comprising: a dispatcher generator configured to generate adispatcher if the dispatcher is determined to not be present for thequery template, the dispatcher including the query compiler caller, thekey generator, and the selectivity region mapper.
 14. The system ofclaim 13, wherein the dispatcher further comprises: a query planselector that is configured to lookup query plans stored in cache forreceived queries; the query interface receiving a second queryconfigured according to the query template; the selectivity regionmapper being configured to map the second query to a second selectivityregion in the selectivity space; the key generator being configured togenerate a second key for the second query that includes an indicationof the second selectivity region; and the query plan selector beingconfigured to determine whether the second key matches any key stored inthe cache, and if a matched key is determined, to select a query plancorresponding to the matched key for execution with respect to thesecond query.
 15. The system of claim 14, wherein if the query planselector determines that the second key does not match any key stored inthe cache, the query compiler caller is configured to request the secondquery to be compiled to generate a second query plan, and the secondquery plan is executed with respect to the second query.
 16. The systemof claim 15, wherein the second query plan is stored in the cache inassociation with the second key.
 17. The system of claim 15, wherein thesecond query plan is stored in the cache in association with the secondkey if the dispatcher determines that the second query plan does notmatch the first query plan; and wherein an indication of the secondselectivity region is stored in association with the first query planand the first key if the dispatcher determines that the second queryplan matches the first query plan.
 18. The system of claim 11, wherein acardinality estimation logic used by the selectivity region mapper todetermine the selectivity region is the same as a cardinality estimationlogic used by the query compiler to compile the query.
 19. The system ofclaim 11, wherein the grid is a logarithmic-scale grid.
 20. A computerstorage medium embedded with computer instructions executed by at leastone processor to perform steps comprising: receiving a query configuredaccording to a query template, the query including values for aplurality of parameters of the query template, the query comprising aplurality of efficiency expressions; managing a plurality of query plansusing a selectivity space by: defining the selectivity space as an arrayof selectivity regions arranged according to a grid that has a number ofdimensions equal to a number of efficiency expressions of the query, thegrid, in accordance with the plurality of efficiency expressions,comprising a multi-dimensional grid divided into a plurality ofmulti-dimensional regions forming the array of selectivity regions,compiling the query to generate a first query plan, determining aselectivity region in the selectivity space for the query, generating akey for the query that includes an indication of the selectivity region,and storing the first query plan in association with the key; andexecuting the first query plan with respect to the query.
 21. Thecomputer storage medium of claim 20, wherein said generating a key forthe query comprises: forming the key to include the query template andthe indication of the selectivity region.
 22. The computer storagemedium of claim 20, further comprising computer instructions executed bythe at least one processor to perform steps comprising: if the generatedkey is determined to match any key of a plurality of keys, selecting aquery plan associated with the matched key, and executing the query planassociated with the matched key with respect to the query.
 23. Thecomputer storage medium of claim 20, wherein if the generated key isdetermined to not match any key of a plurality of keys, storing thefirst query plan in association with the generated key.
 24. The computerstorage medium of claim 20, wherein said compiling comprises: selectinga predetermined point in the selectivity region; and compiling the queryto force selectivities associated with the selected point on respectiveefficiency expressions to generate the first query plan.
 25. Thecomputer storage medium of claim 20, further comprising: determiningthat a dispatcher is not present for the query template; and generatinga dispatcher for the query template in response to determining that adispatcher is not present for the query template; wherein said definingcomprises defining, using the generated dispatcher, the selectivityspace as the array of selectivity regions arranged according to thegrid; wherein said compiling comprises compiling, using the generateddispatcher, the query to generate the first query plan; wherein saiddetermining a selectivity region in the selectivity space for the querycomprises determining, using the generated dispatcher, the selectivityregion in the selectivity space for the query; and wherein saidgenerating comprises generating, using the generated dispatcher, the keyfor the query that includes the indication of the selectivity region.26. The computer storage medium of claim 20, wherein the grid is alogarithmic-scale grid, and said defining comprises: defining theselectivity space as the array of selectivity regions arranged accordingto the logarithmic-scale grid that has the number of dimensions equal tothe number of efficiency expressions of the query.